#include <iostream>
#include <fstream>
#include <cstring>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>

#define MULTICAST_GROUP "239.255.255.250"
#define PORT 8888
#define BUFFER_SIZE 1024

int main()
{
    int sock = socket(AF_INET, SOCK_DGRAM, 0);
    if (sock < 0)
    {
        perror("socket");
        return 1;
    }

    unsigned char ttl = 1;
    setsockopt(sock, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl));

    struct sockaddr_in addr;
    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = inet_addr(MULTICAST_GROUP);
    addr.sin_port = htons(PORT);

    std::ifstream file("large_file.bin", std::ios::binary);
    if (!file)
    {
        std::cerr << "无法打开文件" << std::endl;
        close(sock);
        return 1;
    }

    char buffer[BUFFER_SIZE];
    while (!file.eof())
    {
        file.read(buffer, BUFFER_SIZE);
        ssize_t bytes_read = file.gcount();
        sendto(sock, buffer, bytes_read, 0, (struct sockaddr *)&addr, sizeof(addr));
    }

    const char *end_msg = "END";
    sendto(sock, end_msg, strlen(end_msg), 0, (struct sockaddr *)&addr, sizeof(addr));

    file.close();
    close(sock);
    return 0;
}